import BigNumber from "./bignumber.mjs"
import MD5 from 'blueimp-md5'
import axios from 'axios'
import {
  Dialog
} from 'vant';

axios.defaults.baseURL = "http://xwr.api.haswallow.cn"

//添加请求拦截器
axios.interceptors.request.use(function(config) {
  // 返回配置项
  return config;
}, function(error) {
  //请求错误时做些事
  return false;
});

//添加响应拦截器
axios.interceptors.response.use(function(response) {

  // console.log(JSON.stringify(response));
  if (response['status'] == 200) {
    if (response['data']['code'] == 0) {
      return response['data'];
    } else {
      Dialog.alert({
        title: '消息提醒',
        message: response['data']['msg']
      })
      return false
    }
  } else {
    Dialog.alert({
      title: '消息提醒',
      message: '网络错误！'
    })
  }
  return false;
}, function(error) {
  //请求错误时做些事
  return false;
})



const help = {
  Math: {
    jia: function(l, r) {
      let bul = new BigNumber(l);
      let bur = new BigNumber(r);
      return bul.plus(bur);
    },
    jian: function(l, r) {
      let bul = new BigNumber(l);
      let bur = new BigNumber(r);
      return bul.minus(bur);
    },
    cheng: function(l, r) {
      let bul = new BigNumber(l);
      let bur = new BigNumber(r);
      return bul.multipliedBy(bur);
    },
    chu: function(l, r) {
      let bul = new BigNumber(l);
      let bur = new BigNumber(r);
      return bul.dividedBy(bur);
    }
  },
  Request: {
    get: function(uri, params) {
      return new Promise((resolve, reject) => {
        axios.get(uri, params).then(res => {
          resolve(res);
        }).catch(err => {
          reject(err.data);
        })
      });
    },
    wget: async function(uri, params) {
      let data = await axios.get(uri, params)
      return data
    },
    post: function(uri, params) {},
    put: function(uri, params) {
      return new Promise((resolve, reject) => {
        axios.put(uri, params).then(res => {
          resolve(res);
        }).catch(err => {
          reject(err.data);
        })
      });
    },
    del: function(uri, params) {}
  },
  Message: {
    hint: function(msg) {
      Dialog.alert({
        title: '消息提醒',
        message: msg
      })
    }
  },
  Md5: function(str) {
    return MD5(str)
  },
  TimeStamp: function() {
    return Math.round(new Date() / 1000)
  },
  Cache: function(key = '', value = null, ex = 0) {
    let now = this.TimeStamp()
    key = this.Md5(key)
    if (value == null) {
      let data = localStorage.getItem(key)
      if (data == 'undefined') {
        return false
      }
      let expire = localStorage.getItem(key + "_ex")
      if (expire == 'undefined') {
        return false
      } else {
        if (now > expire && expire != 0) {
          return false
        }
      }
      return JSON.parse(data)
    } else {
      localStorage.setItem(key, JSON.stringify(value))
      localStorage.setItem(key + "_ex", this.Math.jia(ex, now))
      return true
    }
  },
  GetClientele: async function() {
    //获取缓存
    let clientele = this.Cache('clientele');
    console.log(1)
    console.log(clientele)
    console.log(2)
    if (clientele != false) {
      //取到结果直接返回
      return clientele
    }
    console.log(3)
    let res = this.Request.wget('/fule/clientele')
    console.log(res)
    console.log(4)

    console.log(5)
    //缓存请求结果
     // console.log(this.Cache('clientele', res, 3600))
    //返回缓存
    return res
  }
}
export default help
